package com.aaa.controller;


import com.aaa.entity.MyReturn;
import com.aaa.entity.PageSort;
import com.aaa.entity.QuesTion;
import com.aaa.service.SDSQuesTionService;
import com.aaa.util.Enum01;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("QTSDS")
public class QTSDSController extends BaseController{
    @Autowired
//    使用事先定义好的注解标签，对类、方法、属性的特征进行标记，在编译、运行时会找到对应的类、方法、属性，进行标签的执行功能。
    private SDSQuesTionService quesTionServiceSDS;
    MyReturn myReturn=new MyReturn();

//    页面跳转
    @RequestMapping("toShowSDS.do")
    public String toShowUserPage(){
        return "showSDS";
    }

    //查询分页
    @ResponseBody
    @RequestMapping("FindAllQuestionSDS.do")
    public MyReturn FindAllQuestionSDS(PageSort pageSort){
        List<QuesTion> quesTions = quesTionServiceSDS.FindAllSDS(pageSort);
        Page page= (Page) quesTions;
        myReturn.setData(page.getResult());
        myReturn.setCount(page.getTotal());
        return super.getSuccessReturn(myReturn);
    }
    //添加
    @ResponseBody
    @RequestMapping("InsaertQuestionSDS.do")
    public MyReturn InsaertQuestionSDS(QuesTion quesTion){
        quesTion.setCreatetime(new Date());
        Enum01 tion = quesTionServiceSDS.insertQuesTionSDS(quesTion);
        myReturn.setCount((long)tion.getCode());
        myReturn.setMsg(tion.getMsg());
        return myReturn;
    }

//    删除
    @ResponseBody
    @RequestMapping("deleteQuestionSDS.do")
    public MyReturn deleteQuestionSDS(String id){
        boolean b = quesTionServiceSDS.deleteQuesTionSDS(id);
        if (b){
            return super.getSuccessReturn(myReturn);
        }
        return super.getFailReturn(myReturn);
    }
//    修改
    @ResponseBody
    @RequestMapping("updateQuestionSDS.do")
    public MyReturn updateQuestionSDS(QuesTion quesTion){
        quesTion.setUpdatetime(new Date());
        int i = quesTionServiceSDS.updataQuesTionSDS(quesTion);
        if(i>0){
            super.getSuccessReturn(myReturn);
        }else{
            super.getFailReturn(myReturn);
        }
        return myReturn;
    }

//    查重
    @ResponseBody
    @RequestMapping("QuestionrepeatSDS.do")
    public MyReturn QuestionrepeatSDS(QuesTion quesTion){
        boolean quesTionrepeat = quesTionServiceSDS.QuesTionrepeatSDS(quesTion);
        if (quesTionrepeat){
            super.getFailReturn(myReturn);
        }else{
            super.getSuccessReturn(myReturn);
        }
        return myReturn;
    }

    /*
    查询所有，存储题库
     */
    @RequestMapping("questionAllSds.do")
    public String questionAllSds(HttpSession session){
        List<QuesTion> question_sds=quesTionServiceSDS.findAllSds();
        session.setAttribute("question",question_sds);
        return "sdsTest";
    }

}
